#include "scanner_processor.h"
#include <iostream>
#include <string>
#include <event2/http.h>
#include "constants.h"

ScannerProcessor SCANNER_PROCESSOR;

using ScannerProcessorFunc = HttpResponse(ScannerProcessor::*)();
static const std::string FUNC_NAME_ARRAY[2] = { "scanimgs", "scaleimg"};
static ScannerProcessorFunc FUNC_ARRAY[2] = { &ScannerProcessor::ScanImages , &ScannerProcessor::ScaleImage };

HttpResponse ScannerProcessor::DoHttpGet(const HttpRequest& request)
{
	return DoHttpPost(request);
}

HttpResponse ScannerProcessor::DoHttpPost(const HttpRequest& request)
{
	std::string method = FindMethodName4QStr(request);
	//TODO：ignore the args
	bool found = false;
	for (int i = 0; i < sizeof(FUNC_NAME_ARRAY); i++)
	{
		if (method == FUNC_NAME_ARRAY[i])
		{
			return (SCANNER_PROCESSOR.*FUNC_ARRAY[i])();
		}

	}


	std::cout << "ScannerProcessor can't find the method for :" << request.queryStr << std::endl;
	HttpResponse defHttpResp;
	defHttpResp.code = HTTP_INTERNAL;
	defHttpResp.reason = "ScannerProcessor coundnot find the method handler";
	defHttpResp.content = "";
	return defHttpResp;
}

HttpResponse ScannerProcessor::ScaleImage()
{
	std::cout << "scanner processor......ScaleImage" << std::endl;
	HttpResponse http_resp;
		http_resp.code = HTTP_NOTIMPLEMENTED;
		http_resp.reason = "scale image method not implements...";
		http_resp.content = "";
	return http_resp;
}


HttpResponse ScannerProcessor::ScanImages()
{
	std::cout << "scanner processor......ScanImage" << std::endl;
	//todo: call the twain api to generate the image path
	HttpResponse http_resp;
	if (true)
	{
		http_resp.code = HTTP_OK;
		http_resp.reason = "";
		http_resp.content = "['file:///E:/Downloads/00001图片.jpg','file:///E:/Downloads/00002图片.jpg']";
	}
	else
	{
		http_resp.code = HTTP_INTERNAL;
		http_resp.reason = "call scanner api error";
		http_resp.content = "";
	}
	return http_resp;
}